﻿Imports System.Text.RegularExpressions

Public Class frmItemProperties
    Dim row As DataRow

    Public Sub New(ByVal row As DataRow)
        InitializeComponent()

        Me.row = row
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If txtPath.Text = String.Empty Then
            MessageBox.Show("Please provide a valid path.", "Error", MessageBoxButtons.OK)
            txtPath.Focus()
            Return
        End If

        If Not System.IO.Directory.Exists(txtPath.Text) Then
            MessageBox.Show("The folder path provided is not valid.", "Error", MessageBoxButtons.OK)
            txtPath.Focus()
            Return
        Else
            row.Item("abs") = txtPath.Text
        End If

        If txtLabel.Text = String.Empty Then
            MessageBox.Show("Please provide a Label for this folder.", "Error", MessageBoxButtons.OK)
            txtLabel.Focus()
            Return
        Else
            row.Item("lbl") = txtLabel.Text
        End If

        If cboTags.Text = "" Then
            MessageBox.Show("Please choose a Tag.", "Error", MessageBoxButtons.OK)
            cboTags.Focus()
            Return
        End If

        If Not My.Settings.FolderPadLabelList.Contains(cboTags.Text) And cboTags.Text <> "(Always Visible)" Then
            If MessageBox.Show("This Tag does not exist." & vbNewLine & "Do you want to add it now?", "Error", MessageBoxButtons.YesNo) = DialogResult.No Then
                Return
            Else
                My.Settings.FolderPadLabelList.Add(cboTags.Text)
                My.Settings.Save()
            End If
        End If

        row.Item("tag") = cboTags.Text
        row.Item("unc") = ConvertToUncPath(txtPath.Text)

        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Dispose()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Dispose()
    End Sub

    Private Sub frmItemProperties_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not My.Settings.FolderPadLabelList Is Nothing Then
            For Each filter As String In My.Settings.FolderPadLabelList
                cboTags.Properties.Items.Add(filter)
            Next
        End If

        Me.Activate()
    End Sub

    Public Function ConvertToUncPath(ByVal filePath As String) As String
        Dim driveLetter As String = System.IO.Path.GetPathRoot(filePath).Replace("\", "")
        Dim uncPath As String = driveLetter
        Dim query As New System.Management.ObjectQuery("select * from Win32_LogicalDisk")
        Using searcher As New System.Management.ManagementObjectSearcher(query)
            'loops for all drives on system
            For Each Mgmt As System.Management.ManagementObject In searcher.Get()
                'looks for drives with drivetype of 4 which is network drive
                If Mgmt("DriveType").ToString = "4" Then
                    If driveLetter = Mgmt("Caption").ToString Then
                        uncPath = Mgmt("ProviderName").ToString
                        Exit For
                    End If
                End If
            Next
        End Using
        Return filePath.Replace(driveLetter, uncPath)
    End Function
End Class